package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"moon-exam-sys/cache"
	"moon-exam-sys/models"
	"strconv"
	"time"
)

func HomeAdd(c *gin.Context) {
	home_code := c.PostForm("home_code")
	home_name := c.PostForm("home_name")
	home_type := c.PostForm("home_type")

	data := models.Home{
		HomeCode: home_code,
		HomeName: home_name,
		HomeType: home_type,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func OrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	hid, _ := strconv.Atoi(c.PostForm("hid"))

	data := models.HomeOrder{
		OrderCode:  order_code,
		OrderPrice: order_price,
		Hid:        hid,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func HomeOrderList(c *gin.Context) {
	//SELECT
	//home_orders.id as OrderId,
	//homes.home_code as HomeCode,
	//homes.home_name as HomeName,
	//homes.home_type as HomeType,
	//home_orders.order_code as OrderCode,
	//home_orders.order_price as OrderPrice
	//FROM
	//homes
	//LEFT JOIN home_orders ON home_orders.hid = homes.id

	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var list []models.HomeOrderList

	redis_key := "home_order_list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("homes").
			Select(
				"home_orders.id as OrderId",
				"homes.home_code as HomeCode",
				"homes.home_name as HomeName",
				"homes.home_type as HomeType",
				"home_orders.order_code as OrderCode",
				"home_orders.order_price as OrderPrice",
			).
			Offset(offset).
			Limit(size).
			Joins("LEFT JOIN home_orders ON home_orders.hid = homes.id").
			Debug().
			Find(&list)

		jsonData, _ := json.Marshal(list)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var total int64
	models.DB.Model(&models.HomeOrder{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  list,
		"total": total,
	})
	return
}

func HomeOrderDetail(c *gin.Context) {
	id := c.Query("id")

	var detail models.HomeOrderList

	redis_key := "home_order_list_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("homes").
			Select(
				"home_orders.id as OrderId",
				"homes.home_code as HomeCode",
				"homes.home_name as HomeName",
				"homes.home_type as HomeType",
				"home_orders.order_code as OrderCode",
				"home_orders.order_price as OrderPrice",
			).
			Joins("LEFT JOIN home_orders ON home_orders.hid = homes.id").
			Where("home_orders.id = ?", id).
			Debug().
			Find(&detail)

		jsonData, _ := json.Marshal(detail)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &detail)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": detail,
	})
	return

}
